package com.ruanmou.vip.rabbitmq.demo.helloworld;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import com.ruanmou.vip.rabbitmq.demo.util.RabbitMQUtil;

import java.io.IOException;

/**
 * 欢迎同学们来到牧码人教育
 *
 * @Classname Consumer
 * @Description TODO
 * @Date 2020-3-7 10:56
 * @Created by Gerry
 */
public class Consumer {
    public static void main(String[] args) throws IOException {
        // 创建Channel
        Channel channel = RabbitMQUtil.getConnection().createChannel();
        // 定义一个队列
        channel.queueDeclare("hello_queue",false,false,false,null);
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String messageString = new String(message.getBody(), "utf-8");
                System.out.println("接收到的信息:"+ messageString);
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };

        channel.basicConsume("hello_queue",true,deliverCallback, cancelCallback);
    }
}
